require.config({
	 paths:{
	 	"jquery":"jquery",
	 	"cookie":"jquery.cookie",
	 	"common":"xz-common"
	 }
})
$pot=1;
require(["jquery","common","cookie"],function($,cookie){
	
	$(function(){
		//是否登录
        if($.cookie("login")){
        	$("#xz-login").text($.cookie("login")+" ，点击重新登录");
        }else{
        	$("#xz-login").text("【登录】");
        }
     //退出
      $("#xz-login").mousedown(function(){
      	$.removeCookie("login",{expires:0,path:"/"});
      	location.href = "../html/login.html";
      })
	})
	  //左侧手风琴
 $(function(){
 	     
	  	$(".left-son h2 span").toggle(function(){
	  	$(this).parent().siblings("ul").css("display","block");
	  	},function(){
	  	$(this).parent().siblings("ul").css("display","none");
	  	}
	  	
	  	)
	  })
 //顶部手风琴
 $(function(){
 	  $(".right-select #more").toggle(function(){
 	  	$(this).parent().css("height","80px");
 	  	 $(this).text("收起");
 	  },
 	    function(){
 	     $(this).parent().css("height","40px");
 	     $(this).text("更多");
 	    }
 	  )
 	  $(".more2").toggle(function(){
 	  	$("#hiddenmore").css("display","block");
 	  	$(".more2").html("收起选项");
 	  },function(){
 	  	$("#hiddenmore").css("display","none");
 	  	$(".more2").html("更多选项");
 	  })
 	 })
 //加入购物车
 $(function(){
               setTimeout(function(){
               	$(".pay-shopcar").each(function(i) {
			   $(".pay-shopcar").eq(i).mousedown(function() {
				$.getJSON("../xz.json", function(obj) {
					var goods = $.cookie("goods") ? $.cookie("goods") : "";
					var id = $(".list-goodswarp").eq(i).attr("id");
					var obj1 = converStrToObj(goods);
                   
					if(id in obj1) {
						obj1[id].num = parseInt(obj1[id].num) + 1;
					} else {
						obj1[id] = {
							name: obj.main_son1[i+6].main_son1_h4_font,
							pirce: obj.main_son1[i+6].content_salepirce_now,
							integral: obj.main_son1[i+6].content_integral,
							imgsrc: "../" + obj.main_son1[i+6].son1_photo,
							num: 1
						}
					}
					$.cookie("goods", convertObjTostr(obj1), {
						expires: 7,
						path: "/"
					});
					alert("加入成功!");
				var goods = converStrToObj($.cookie("goods"));
				var goodsnum = 0;
				for(val in goods) {
					goodsnum += parseInt(goods[val].num);
				}
				$(".shopcar").text(goodsnum);
					
					
				})
			
			})
     })
            $(".list-bigimg").each(function(i) {
			$(".list-bigimg").eq(i).mousedown(function() {
				$.cookie("product", i+6,{expires:7,path:"/"});
				location.href = "../html/productdetail.html";
			})

		})
               },500)
     			
});
 //动态加载数据
 $(function(){
 	 $.getJSON("../xz.json",function(obj){
 	 	for(var i=6; i< obj.main_son1.length;i++){
 	 		$(".list-goods").append(
 	 			       '<li><div class="list-goodswarp" id='+obj.main_son1[i].content_id +'>'
	     				+'<div class="shadow1">收藏</div>'
	     				+'<p class="list-bigimg">'
	     				+'<img src="../'+obj.main_son1[i].son1_photo+'"/>'
	     				+'</p>'
	     				+'<p class="goods-describe">'
	     				+	obj.main_son1[i].main_son1_h4_font
	     				+'</p>'
	     				+'<p class="goods-pirce">'
	     				+	obj.main_son1[i].content_salepirce_now
	     				+'	<span>'+obj.main_son1[i].content_salepirce_old+'</span>'
	     				+'</p>'
	     			   +' <p class="go-shopcar">'
	     			    +	'<span>最近0人成交</span>'
	     			    +	'<input type="button" class="pay-shopcar" />'
	     			   +' </p>'
//	     			   + '<img src="../img/bigprompt.png" class="bigprompt"/>'
	     			   + '<p class="goodstalk">好评率<span>100%</span></p>'
	     			+'</div></li>')
                  };
 	 		     for(var i=6; i< 15;i++){
 	 			$("#hot-top .xz-recommend-warp").append(
 	 				' <div class="xz-recommend-son">'
        	 		+'<a href="#" class="xz-recommend-image" >'
        	 		+	'<img src="../'+obj.main_son1[i].son1_photo+'"/>'
        	 	+	'</a>'
        	 	 +' <p class="xz-recommend-top">'
        	 	   + '	<a href="#">'+obj.main_son1[i].main_son1_h4_font+'</a>'
        	 	  +'  </p>'
        	 	   +' <p class="xz-recommend-bottom">'
        	 	    +	obj.main_son1[i].content_salepirce_now
        	 	    +'<span>'
        	 	    +obj.main_son1[i].content_salepirce_old
        	 	    +'</span>'
        	 	    +'</p>'			
 	 		)}
 	 		  for(var i=16; i< obj.main_son1.length;i++){
 	 			$("#like-you .xz-recommend-warp").append(
 	 				' <div class="xz-recommend-son">'
        	 		+'<a href="#" class="xz-recommend-image" >'
        	 		+	'<img src="../'+obj.main_son1[i].son1_photo+'"/>'
        	 	+	'</a>'
        	 	 +' <p class="xz-recommend-top">'
        	 	   + '	<a href="#">'+obj.main_son1[i].main_son1_h4_font+'</a>'
        	 	  +'  </p>'
        	 	   +' <p class="xz-recommend-bottom">'
        	 	    +	obj.main_son1[i].content_salepirce_now
        	 	    +'<span>'
        	 	    +obj.main_son1[i].content_salepirce_old
        	 	    +'</span>'
        	 	    +'</p>'			
 	 		)}
 	 	
 	 })
 })
 $(function(){
 	 $("body").mouseover(function(){
 	 	$(".list-goods li").mouseover(function(){
 	 		$(this).children(".bigprompt").css("display","block");
 	 	})
 	 })
 })
 //页面跳转
 $(function(){
 	     //购物车跳转
    $("#xz-shopcar").mousedown(function(){
    	location.href = "../html/shopcar.html"
    	
    });
    //注册页跳转
    $("#toresgister").mousedown(function(){
    	location.href = "../html/register.html"
    
    });    
    //登录页跳转
     $("#xz-login").mousedown(function(){
    	location.href = "../html/login.html"
    	
    })		
    //列表页跳转
     $(".xz-listofgoods-one a").mousedown(function(){
     	location.href = "../html/listgoods.html"
     	
     })
 })
 //字符串转对象
	function converStrToObj(str) {

		var obj = {};
		if(!str) {
			return {};
		}
		var arr = str.split(":");
		for(var i = 0; i < arr.length; i++) {
			arr2 = arr[i].split(",");
			obj[arr2[0]] = {
				name: arr2[1],
				pirce: arr2[2],
				num: arr2[3],
				integral: arr2[4],
				imgsrc: arr2[5]

			}
		}
		return obj;
	}
	//对象转字符串
	function convertObjTostr(obj) {
		var str = "";
		for(val in obj) {
			if(str) {
				str += ":";
			}
			str += val + "," + obj[val].name + "," + obj[val].pirce + "," + obj[val].num + "," +
				obj[val].integral + "," + obj[val].imgsrc
		}
		return str;
	}
})


